package com.desire.homework;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.GeneralPath;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;

/**
 * @author desire
 */
public class _45_验证码 {
    public static void main(String[] args) throws IOException, FontFormatException {
        int w = 160;
        int h = 60;
        int len = 4;
        Random rand = new Random();
        BufferedImage i = new BufferedImage(w, h, 2);
        Graphics2D g = i.createGraphics();
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_DEFAULT);

        // 设置颜色 画矩形
        g.setColor(new Color(251, 251, 251));
        g.fillRect(0, 0, w, h);

        //设置字体
        var font = new Font("宋体", Font.BOLD, (int) 55f);
        try {
            var is = Thread.currentThread().getContextClassLoader().getResourceAsStream("fonts/f.ttf");
            font = Font.createFont(Font.TRUETYPE_FONT, is);
            font = font.deriveFont(Font.BOLD, 55f);
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 将设置好的字体设置进去
        g.setFont(font);


        // 准备验证码库 干扰字设置
        String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        double radianPercent = 0D;
        for (int n = 0; n < 20; n++) {
            font = font.deriveFont(Font.PLAIN, rand.nextFloat() * 15 + 15);
            g.setFont(font);
            g.setColor(new Color(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255), rand.nextInt(10) + 50));
            int x = rand.nextInt(w);
            int y = rand.nextInt(h);
            g.drawString(String.valueOf(str.charAt(rand.nextInt(str.length()))), x, y);
        }

        // 验证码
        StringBuilder sb = new StringBuilder(len);
        g.setColor(new Color(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255), rand.nextInt(25) + 220));
        for (int n = 0; n < len; n++) {
            font = font.deriveFont(Font.PLAIN, rand.nextFloat() * 35 + 30);
            g.setFont(font);
            int x = n * 32 + 5;
            int y = rand.nextInt(35) + 20;
            radianPercent = Math.PI * (rand.nextInt(35) / 180D);
            if (rand.nextBoolean()) {
                radianPercent = -radianPercent;
            }
            g.rotate(radianPercent, x, y);
            String sss = String.valueOf(str.charAt(rand.nextInt(str.length())));
            sb.append(sss);
            g.drawString(sss, x, y);
            g.rotate(-radianPercent, x, y);
        }

        // 干扰线
        Point[] points = {
                new Point(0, 0),
                new Point(10, 50),
                new Point(30, 6),
                new Point(60, 55),
                new Point(80, 3),
                new Point(160, 60)
        };
        GeneralPath path = new GeneralPath();
        path.moveTo(points[0].x, points[0].y);
        for (int i2 = 0; i2 < points.length - 1; ++i2) {
            Point sp = points[i2];
            Point ep = points[i2 + 1];
            Point c1 = new Point((sp.x + ep.x) / 2, sp.y);
            Point c2 = new Point((sp.x + ep.x) / 2, ep.y);
            path.curveTo(c1.x, c1.y, c2.x, c2.y, ep.x, ep.y);
        }
        g.setStroke(new BasicStroke(rand.nextInt(6) + 2));

        g.setColor(new Color(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255), rand.nextInt(25) + 220));
        int yy = rand.nextInt(15) + 15;
        int hh = rand.nextInt(20) + 10;
        int aa = rand.nextInt(60) + 20;
        for (int x = 10; x < w; x++) {
            int y = (int) (yy + hh * Math.sin(x * Math.PI / aa));
            g.fillOval(x, y, 3, 3);
        }

        // 输出验证码
        System.out.println(sb);

        g.dispose();
        String p = "C:\\Users\\desire\\Desktop\\javaProject\\01-javaBase\\src\\main\\resources\\验证码2.png";
        Runtime runtime = Runtime.getRuntime();
        try {
            ImageIO.write(i, "png", new File(p));
            runtime.exec("cmd /k start " + p);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
